package me.json.other;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

public class Province {

    private String id;

    private String areaId;

    private String areaName;

    private List<City> subAreas;

    public Province(String id, String areaId, String areaName, List<City> subAreas) {
        super();
        this.id = id;
        this.areaId = areaId;
        this.areaName = areaName;
        this.subAreas = subAreas;
    }

    public Province() {
        super();
    }

    public static Province fromJsonParser(JsonParser jsonParser) throws JsonParseException,
            IOException {

        Province province = null;

        if (jsonParser.getCurrentToken() != JsonToken.VALUE_NULL) {

            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {

                String fieldName = jsonParser.getCurrentName();

                if (fieldName == null) {
                    continue;
                }

                if (province == null) {
                    province = new Province();
                }

                if ("id".equals(fieldName)) {
                    jsonParser.nextToken();
                    province.id = jsonParser.getText();
                    continue;
                }

                if ("areaId".equals(fieldName)) {
                    jsonParser.nextToken();
                    province.areaId = jsonParser.getText();
                    continue;
                }

                if ("areaName".equals(fieldName)) {
                    jsonParser.nextToken();
                    province.areaName = jsonParser.getText();
                    continue;
                }

                if ("subAreas".equals(fieldName)) {
                    jsonParser.nextToken();
                    province.subAreas = new ArrayList<City>();
                    while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                        City city = City.fromJsonParser(jsonParser);
                        province.subAreas.add(city);
                    }
                    continue;
                }

                jsonParser.skipChildren();
            }
        }

        return province;
    }

    public String getAreaId() {
        return areaId;
    }

    public void setAreaId(String areaId) {
        this.areaId = areaId;
    }

    public String getAreaName() {
        return areaName;
    }

    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    public List<City> getSubAreas() {
        return subAreas;
    }

    public void setSubAreas(List<City> subAreas) {
        this.subAreas = subAreas;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return areaName;
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof Province) {
            Province province = (Province) o;
            if (getId().equals(province.getId())) {
                return true;
            } else {
                return false;
            }
        }
        return super.equals(o);
    }
}
